package com.uchengwang.ui.cm_ui.login.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/2/7.
 */

public class ChooseCycleAdapter extends BaseAdapter{
    private Context context;
    private List<CycleBean> list = new ArrayList<>();
    public ChooseCycleAdapter(Context context){
        this.context = context;
    }

    public void setList(List<CycleBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<CycleBean> getList(){
        return list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ChooseVH holder = null;
        if (view ==null){
            holder = new ChooseVH();
            view = LayoutInflater.from(context).inflate(R.layout.layout_choose_cycle_view, null);
            holder.choose_ln = (LinearLayout) view.findViewById(R.id.choose_ln);
            holder.choose_image = (ImageView) view.findViewById(R.id.choose_image);
            holder.choose_tv = (TextView) view.findViewById(R.id.choose_tv);
            holder.choose_icon = (ImageView) view.findViewById(R.id.choose_icon);

            view.setTag(holder);
            AutoUtils.autoSize(view);
        }else {
            holder = (ChooseVH) view.getTag();
        }
        final CycleBean bean = list.get(position);
        ImageManager.getInstance().setRoundSize(14).loadImage(context, holder.choose_image, bean.getCycleCover(), ImageManager.ImageType.ROUND);
        holder.choose_tv.setText(bean.getCycleName());

        if (bean.isSub()){
            ImageManager.getInstance().loadImage(context, holder.choose_icon, R.drawable.icon_cycle_sub_choose, ImageManager.ImageType.NARMOL);
        }else {
            ImageManager.getInstance().loadImage(context, holder.choose_icon, R.drawable.icon_cycle_sub_normal, ImageManager.ImageType.NARMOL);
        }

        final ChooseVH finalHolder = holder;
        holder.choose_ln.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                subCycle(bean, finalHolder.choose_icon);
            }
        });
        return view;
    }

    private class ChooseVH{
        private LinearLayout choose_ln;
        private ImageView choose_image, choose_icon;
        private TextView choose_tv;
    }

    /**
     * 订阅某个社圈
     * */
    private void subCycle(final CycleBean cycleBean, final ImageView holder){
        CycleHttpUtils.getUtils().subOrDeleteCycle((Activity) context, !cycleBean.isSub(), cycleBean.getId(), cycleBean.getUniId(), new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (stutas ==0){
                    cycleBean.setSub(!cycleBean.isSub());
                    if (cycleBean.isSub()){
                        ImageManager.getInstance().loadImage(context, holder, R.drawable.icon_cycle_sub_choose, ImageManager.ImageType.NARMOL);
                    }else {
                        ImageManager.getInstance().loadImage(context, holder, R.drawable.icon_cycle_sub_normal, ImageManager.ImageType.NARMOL);
                    }
                }else {
                    UCommonUtil.getUtil().showToast(context, "请求失败");
                }
            }
        });
    }
}
